package com.an.vhr.controller;

import com.an.vhr.bean.ChatMessage;
import com.an.vhr.bean.Hr;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author: an1089
 **/
@Controller
public class WsController {
    @Resource
    SimpMessagingTemplate simpMessagingTemplate;

    @MessageMapping("/ws/chat")
    public void handleMsg(Authentication authentication, ChatMessage chatMessage){
        Hr principal = (Hr) authentication.getPrincipal();
        chatMessage.setFrom(principal.getUsername());
        chatMessage.setFromName(principal.getName());
        chatMessage.setDate(new Date());
        simpMessagingTemplate.convertAndSendToUser(chatMessage.getTo(),"/queue/chat",chatMessage);
    }
}
